using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;

namespace RealDolmen.SharePoint.ListFeatures.Controls
{
    public class ScheduleSelector : WebControl, IPostBackEventHandler
    {
        #region Fields
        private HiddenField hiddenField;
        private LinkButton linkButton; 
        #endregion // Fields

        #region Properties
        public string ScheduleString
        {
            get
            {
                return hiddenField.Value;
            }
            set
            {
                EnsureChildControls();
                hiddenField.Value = value;
                linkButton.Text = hiddenField.Value;
                linkButton.OnClientClick = Url;
            }
        }
        protected string Url
        {
            get
            {
                string url = string.Format("ScheduleSelector.aspx?Value={1}",
                SPContext.Current.Web,
                string.Format("'+document.getElementById('{0}').value+'", ID));

                string winParams = "dialogWidth:400px;dialogHeight:300px;resizable:yes";
                string callBack = string.Format("function(rv){{if(rv){{document.getElementById('{0}').value = rv;document.getElementById('{0}link').innerText = rv}}}}", ID);

                return string.Format("commonShowModalDialog('{0}','{1}',{2}, null);return false",
                url, winParams, callBack);
            }
        } 
        #endregion // Properties

        #region Overrides
        protected override void CreateChildControls()
        {
            hiddenField = new HiddenField();
            hiddenField.ID = ID;

            linkButton = new LinkButton();
            linkButton.ID = ID + "link";

            Controls.Add(linkButton);
            Controls.Add(hiddenField);
        }


        public override void RenderControl(HtmlTextWriter writer)
        {
            linkButton.RenderControl(writer);
            hiddenField.RenderControl(writer);
        } 
        #endregion // Overrides

        // ScheduleChanged event needs to be implemented again.
        #region IPostBackEventHandler Members
        // public event EventHandler ScheduleChanged;

        public void RaisePostBackEvent(string eventArgument)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }
}
